package com.mobiletrain.org.motherkitch;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;


public class SearchActivity extends ActionBarActivity {

    private Toolbar mToolbar;
    private EditText mContent;
    private ImageView mSearch;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);
        initActionBar();

    }


    public void initActionBar() {
        mToolbar = (Toolbar) findViewById(R.id.searchbar);
        setSupportActionBar(mToolbar);
        mToolbar.setNavigationIcon(R.drawable.back);
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mContent = (EditText) mToolbar.findViewById(R.id.search_content);

        mSearch = (ImageView) mToolbar.findViewById(R.id.search);

        mSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String msg=mContent.getText().toString();
                if(msg!=null){
                    searchResult(1,msg);
                }
            }
        });

    }


    public void searchResult(int page, String msg) {
        String path = "http://www.qubaobei.com/ios/cf/dish_list.php?stage_id=" + 1+ "&page=1&limit=20";
        HttpUtils httpUtils = new HttpUtils();
//        try {
//            msg = URLEncoder.encode(msg, "UTF-8");
//        } catch (UnsupportedEncodingException e) {
//            e.printStackTrace();
//        }
        RequestParams params = new RequestParams();
        Toast.makeText(SearchActivity.this, msg, Toast.LENGTH_SHORT).show();
        params.addQueryStringParameter("page", page + "");
        params.addQueryStringParameter("title", msg + "");
        params.addQueryStringParameter("limit", (20 * page) + "");
        httpUtils.send(HttpRequest.HttpMethod.POST, path, params, new RequestCallBack<Object>() {
            @Override
            public void onSuccess(ResponseInfo<Object> objectResponseInfo) {
                String json = objectResponseInfo.result.toString();
                Toast.makeText(SearchActivity.this, json, Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onFailure(HttpException e, String s) {
                Toast.makeText(SearchActivity.this, s, Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_search, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
